package com.shanghaionstar.home.presenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.PlateEngineNumEntity;
import com.onstar.cn.mag.enumtype.CategoryType;
import com.onstar.cn.mag.publicService.response.H5DispatcherResEntity;
import com.onstar.cn.mag.publicService.response.PreferentialResEntity;
import com.onstar.cn.mag.publicService.response.ResponBannerEntity;

import com.shanghaionstar.R;
import com.shanghaionstar.activity.webview.handler.CheYinInsuranceHandler;
import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.activity.webview.handler.IWebType;
import com.shanghaionstar.activity.webview.handler.WebDataEntity;
import com.shanghaionstar.discover.service.DiscoverService;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.home.iview.IAssuranceView;
import com.shanghaionstar.user.service.PlateEngineService;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BannerUtil;

import java.util.List;

import rx.functions.Action1;

/**
 * Created by 7828wc on 16/6/29.
 */
public class AssurancePresenter extends BasePresenter<PlateEngineNumEntity> {
    private PlateEngineService service;
    private DiscoverService mService;
    private IAssuranceView mAssuranceView;

    public AssurancePresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<PlateEngineNumEntity> view) {
        super.setView(view);
        mAssuranceView = (IAssuranceView) view;
        service = new PlateEngineService(view.onActivity());
        mService = new DiscoverService(view.onActivity());
        setServiceProvider(service);
    }

    public void commandData() {
        BannerUtil util = new BannerUtil(mAssuranceView.onActivity());
        subscription =  util.commandDataList(CategoryType.INSURANCE_BANNER.value()).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<List<ResponBannerEntity>>() {
                    @Override
                    public void call(List<ResponBannerEntity> response) {
                        if (response != null && response.size() > 0) {
                            mAssuranceView.onGetBanner(response);
                        } else {
                            mAssuranceView.onGetBanner(null);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (throwable != null && mAssuranceView != null)
                            mAssuranceView.onGetBanner(null);
                    }
                });
    }

    public void onBannerClick(ResponBannerEntity entity){
        BannerUtil util =  new BannerUtil(mAssuranceView.onActivity());
        util.bannerClick(entity,mAssuranceView.onActivity(), FunctionNameFindId.UnSend,mvpScheduler,mAssuranceView);
    }


    public void updateVehicleInfo(String insuranceComp, String compulsoryInsuranceExpireDate, String businessInsuranceExpireDate,boolean isQuery) {
        PlateEngineNumEntity plateEngineNumEntity = new PlateEngineNumEntity();
        plateEngineNumEntity.setAccountID(ApplicationUtil.getInstance().getAccountId());
        plateEngineNumEntity.setVin(ApplicationUtil.getInstance().getVin());
        if (!CommonUtil.isNull(insuranceComp)) {
            plateEngineNumEntity.setInsuranceComp(insuranceComp);
        }
        if (!CommonUtil.isNull(compulsoryInsuranceExpireDate)) {
            plateEngineNumEntity.setCompulsoryInsuranceExpireDate(compulsoryInsuranceExpireDate);
        }
        if (!CommonUtil.isNull(businessInsuranceExpireDate)) {
            plateEngineNumEntity.setBusinessInsuranceExpireDate(businessInsuranceExpireDate);
        }
        service.setPlateEngineNumEntity(plateEngineNumEntity, isQuery);
    }


    public void getH5(final PreferentialResEntity entity) {
        view.setLoading(true);
        BannerUtil bannerUtil = new BannerUtil(view.onActivity());
        subscription = bannerUtil.h5Dispatcher(entity).subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Action1<H5DispatcherResEntity>() {
                    @Override
                    public void call(H5DispatcherResEntity mResult) {
                        view.setLoading(false);
                        String body = mResult.getJsonStr();
                        String url = mResult.getUrl();
                        if (!TextUtils.isEmpty(url)&& !TextUtils.isEmpty(body))
                            launchCheYin(url,body);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //抛出Error异常
                        view.setLoading(false);
                    }
                });
    }

    /**
     * 车音网调用
     * @param url
     */
    public void launchCheYin(String url,String body) {
//        Intent intent2 = new Intent(view.onActivity(), CheYinWebActivity.class);
//        intent2.putExtra("url", url);
//        intent2.putExtra("body", body);
//        view.onActivity().startActivity(intent2);
        Intent intent2 = new Intent(view.onActivity(), CommonWebViewActivity.class);
        Bundle bundle = new Bundle();
        WebDataEntity mWebData = new WebDataEntity();
        mWebData.setLinkUrl(url);
        mWebData.setTopTitle(view.onActivity().getString(R.string.assurance_order));
        mWebData.setClzName(CheYinInsuranceHandler.class.getName());
        mWebData.what = IWebType.TYPE_CHE_YIN_INSURANCE;
        bundle.putParcelable(IWebType.KEY_WEB_DATA,mWebData);
        bundle.putString(CheYinInsuranceHandler.PARAM_BODY,body);
        intent2.putExtras(bundle);
//        HashMap<String, String> webViewParam = new HashMap<>();
//        webViewParam.put(OnstarWebViewActivity.URL, url);
//        webViewParam.put(CheYinInsuranceHandler.PARAM_BODY, body);
//
//        intent2.putExtra(OnstarWebViewActivity.WEBVIEWS_PROXY_NAME, CheYinInsuranceWebViewProxy.class);
//        intent2.putExtra(OnstarWebViewActivity.WEBVIEWS_MAP,webViewParam);

        view.onActivity().startActivity(intent2);

    }

}
